package com.elangzhi.ssm.modules.talk.app;

import com.elangzhi.ssm.controller.json.Tip;
import com.elangzhi.ssm.model.Demand;
import com.elangzhi.ssm.model.Image;
import com.elangzhi.ssm.model.Record;
import com.elangzhi.ssm.model.Talk;
import com.elangzhi.ssm.modules.image.services.ImageService;
import com.elangzhi.ssm.modules.message.services.MessageService;
import com.elangzhi.ssm.modules.record.services.RecordService;
import com.elangzhi.ssm.modules.talk.services.TalkService;
import com.elangzhi.ssm.modules.user.services.UserService;
import com.elangzhi.ssm.tools.UUIDFactory;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* 说说 客户端 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/app/talk")
public class AppTalkController {


    /**
     * 获取说说列表 传省市区参数
     * @param talk 参数
     * @param page 第几页
     * @param size 每页大小
     * @return 列表
     */
    @RequestMapping("/listByRegion")
    @ResponseBody
    public PageInfo<Talk> listByRegion(Talk talk,
                                         @RequestParam Integer page,
                                         @RequestParam Integer size){
        PageInfo<Talk> talks = talkService.listByRegion(talk,page,size);

        //填充图片、用户信息
        for(Talk t : talks.getList()){
            t.setBannerList(imageService.listByTypeInfo(4,t.getId()));
            t.setUser(userService.selectById(t.getUserId()));
            // 获取赞列表
            List<Record> zanList = recordService.listByTypeAdnInfoId(0,t.getId());
            for(Record record : zanList){
                record.setUser(userService.selectById(record.getUserId()));
            }
            t.setZanList(zanList);

            //获取评论列表
            List<Record> recordList = recordService.listByTypeAdnInfoId(1,t.getId());
            for(Record record : recordList){
                record.setUser(userService.selectById(record.getUserId()));

                // 获取子评论列表
                List<Record> childList = recordService.listByTypeAdnInfoId(2,record.getId());
                for(Record child : childList){
                    child.setUser(userService.selectById(child.getUserId()));
                    child.setReceiveUser(userService.selectById(child.getReceiveId()));
                }
                record.setChild(childList);
            }
            t.setRecordList(recordList);
        }
        return talks;
    }

    /**
     * 删除说说
     * @param id 说说id
     * @return 成功状态
     */
    @RequestMapping("/deleteTalk")
    @ResponseBody
    public Tip deleteTalk(Long id){
        try {
            //删除赞和通知
            List<Record> zanList = recordService.listByTypeAdnInfoId(1,id);
            for(Record record : zanList){
                recordService.deleteById(record.getId());
                messageService.deleteByRecord(record.getId());
            }

            //删除评论和通知
            List<Record> recordList = recordService.listByTypeAdnInfoId(1,id);
            for(Record record : recordList){
                // 删除子评论和通知
                List<Record> childList = recordService.listByTypeAdnInfoId(2,record.getId());
                for(Record child : childList){
                    recordService.deleteById(child.getId());
                    messageService.deleteByRecord(child.getId());
                }
                recordService.deleteById(record.getId());
                messageService.deleteByRecord(record.getId());
            }
            // 删除talk
            talkService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            System.out.println("删除说说失败");
            return new Tip(1,"删除说说失败");
        }
    }

    /**
     * 修改说说内容
     * @param talk 说说
     * @param files 新图片
     * @param request 请求
     * @return 成功状态
     */
    @RequestMapping("/updateTalk")
    @ResponseBody
    public Tip updateTalk(Talk talk,@RequestParam(required = false)  MultipartFile[] files, HttpServletRequest request){
        try {
            talkService.updateById(talk);
            saveImg(talk.getId(),files,request);
            return new Tip();
        } catch (Exception e) {
            System.out.println("说说修改失败");
            return new Tip(1);
        }
    }

    /**
     * 添加说说
     * @param talk 说说内容
     * @param files 图片
     * @param request 请求
     * @return 成功状态
     */
    @RequestMapping("/submit")
    @ResponseBody
    public Tip submit(Talk talk,@RequestParam(required = false)  MultipartFile[] files, HttpServletRequest request){
        talk.setId(UUIDFactory.getLongId());
        talk.setSetTime(new Date());
        try {
            talkService.insert(talk);
            saveImg(talk.getId(),files,request);
            return new Tip();
        } catch (Exception e) {
            System.out.println("说说添加失败");
            return new Tip(1);
        }
    }

    /**
     * 保存图片
     * @param talkId 说说id
     * @param files 需求图片
     */
    public void saveImg(Long talkId, MultipartFile[] files, HttpServletRequest request){
        if (files != null) {
            try {
                for(MultipartFile file : files){
                    String fileName = file.getOriginalFilename();
                    String saveName = UUIDFactory.getLongId() + fileName.substring(fileName.lastIndexOf("."));
                    String url = "/images/talk/" + saveName;
                    String realPath = request.getServletContext().getRealPath(url);
                    file.transferTo(new File(realPath));

                    Image image = new Image();
                    image.setId(UUIDFactory.getLongId());
                    image.setInfo(talkId);
                    image.setType(4);
                    image.setUrl(url);
                    imageService.insert(image);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 删除图片
     * @param id 图片id
     * @return 删除状态
     */
    @RequestMapping("/deleteImg")
    @ResponseBody
    public Tip deleteImg(Long id){
        try {
            imageService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            System.out.println("删除说说图片失败");
            return new Tip(1);
        }
    }




    //---------------------------- property -------------------------------

    @Resource
    private TalkService talkService;

    @Resource
    private ImageService imageService;

    @Resource
    private UserService userService;

    @Resource
    private RecordService recordService;

    @Resource
    private MessageService messageService;

}
